package com.kkb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.DoctorMapper;
import com.kkb.pojo.Doctor;
import com.kkb.pojo.DoctorExample;
import com.kkb.pojo.Hosregister;
import com.kkb.vo.DoctorQueryVO;
import com.kkb.vo.RegistrationQueryVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class DoctorService {
    @Resource
    private DoctorMapper doctorMapper;

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public PageInfo<Doctor> queryByPage(Integer pageNum, Integer pageSize, DoctorQueryVO vo) {
        DoctorExample doctorExample = new DoctorExample();
        DoctorExample.Criteria criteria = doctorExample.createCriteria();
        if (vo != null) {
            if (vo.getId() != null) {
                criteria.andIdEqualTo(vo.getId());
            }
            if (vo.getName() != null && !"".equals(vo.getName().trim())) {
                criteria.andNameEqualTo(vo.getName().trim());
            }
            if (vo.getKeShi() != null && !"".equals(vo.getKeShi().trim())) {
                criteria.andKeShiEqualTo(vo.getKeShi().trim());
            }
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Doctor> doctors = doctorMapper.selectByExample(doctorExample);
        return new PageInfo<>(doctors);
    }

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public Doctor queryById(Integer id) {
        Doctor doctor = doctorMapper.selectByPrimaryKey(id);
        return doctor;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int updateDoctor(Doctor doctor) {
        return doctorMapper.updateByPrimaryKeySelective(doctor);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int addDoctor(Doctor doctor) {
        doctor.setState(0);
        doctor.setInTime(new Date());
        return doctorMapper.insertSelective(doctor);
    }
}
